package com.gf.user.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.base.type.Activity;
import com.gf.base.type.BloodType;
import com.gf.base.type.LicenseState;
import com.gf.base.type.Sex;
import com.gf.user.dao.UserDao;

@Service("userDao")
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * update user set portrait=? where id=?
	 */
	@Value("${userDao.updateProtraitSql}")
	private String updateProtraitSql;

	/**
	 * select portrait from user where id=?
	 */
	@Value("${userDao.queryProtraitSql}")
	private String queryProtraitSql;

	/**
	 * select id,nickname from user
	 */
	@Value("${userDao.queryAllNicknameAndUidForAutocompleteSql}")
	private String queryAllNicknameAndUidForAutocompleteSql;

	/**
	 * update user set sex=?,bloodType=?,income=?,experience=? where id=?
	 */
	@Value("${userDao.updateSetup}")
	private String updateSetupSql;

	/**
	 * insert into mobile (userId,number,code,createtime) values
	 * (?,?,current_timestamp)
	 */
	@Value("${userDao.insertMobileCodeSql}")
	private String insertMobileCodeSql;

	/**
	 * userDao.queryMobileCodeSql=select (id,number,createtime) from mobile
	 * where userId=? and code=? and activity=? order by createtime desc limit 1
	 */
	@Value("${userDao.queryMobileCodeSql}")
	private String queryMobileCodeSql;

	/**
	 * update mobile set activity=? where id=?
	 */
	@Value("${userDao.updateMobileActivitySql}")
	private String updateMobileActivitySql;

	/**
	 * update user set mobile=? where id=?
	 */
	@Value("${userDao.updateUserMobileSql}")
	private String updateUserMobileSql;

	/**
	 * update user set licensePath=? where id=?
	 */
	@Value("${userDao.insertLicenseSql}")
	private String insertLicenseSql;

	/**
	 * update user set lastLoginIp=?,lastLoginTime=current_timestamp where id=?
	 */
	@Value("${userDao.updateLoginUserInfoSql}")
	private String updateLoginUserInfoSql;

	@Override
	public void updateProtrait(long id, String protraitUrl) {
		jdbcTemplate.update(updateProtraitSql, protraitUrl, id);
	}

	@Override
	public String queryProtrait(long id) {
		return (String) jdbcTemplate.queryForObject(queryProtraitSql,
				new Object[] { id }, String.class);
	}

	@Override
	public List<Map<String, Object>> queryAllNicknameAndUidForAutocomplete() {
		return jdbcTemplate
				.queryForList(queryAllNicknameAndUidForAutocompleteSql);
	}

	@Override
	public boolean updateSetup(Long userId, Sex sex, BloodType bloodType,
			String income, String experience) {
		int result = jdbcTemplate.update(updateSetupSql, sex.toString(),
				bloodType.toString(), income, experience, userId);
		return result > 0 ? true : false;
	}

	@Override
	public long insertMobileCode(long userId, String number, String code) {

		return this.insertWithIdReturn(insertMobileCodeSql, userId, number,
				code);
	}

	@Override
	public Map<String, Object> queryMobileCode(long userId, String code) {
		return jdbcTemplate.queryForMap(queryMobileCodeSql, userId, code,
				Activity.NORMAL.toString());
	}

	@Override
	public void updateMobileActivity(long id, Activity activity) {
		jdbcTemplate.update(updateMobileActivitySql, activity.toString(), id);
	}

	@Override
	public void updateUserMobile(long userId, String mobile) {
		jdbcTemplate.update(updateUserMobileSql, mobile, userId);
	}

	@Override
	public void insertLicense(long userId, String newPath,
			LicenseState licenseState) {
		jdbcTemplate.update(insertLicenseSql, newPath, licenseState.toString(),
				userId);
	}

	@Override
	public void updateLoginUserInfo(long userId, String ip) {
		jdbcTemplate.update(updateLoginUserInfoSql, ip, userId);
	}

}
